package com.cat.controller;

import com.cat.model.Member;
import com.cat.service.MemberService;
import com.cat.utils.EUDataGridResult;
import com.cat.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by lin on 17-9-7.
 */
@RestController
public class MemebrController {
    @Autowired
    private MemberService memberService;

    @PostMapping("/members/")
    public Response add(Member member){
        Response response = new Response();
        int num = memberService.add(member);
        if(num <= 0){
            response.setError();
            return response;
        }
        response.setSuccess();
        return  response;
    }

    @GetMapping("/members/{id}")
    public Response getOne(@PathVariable Integer id){
        Response response = new Response();
        Member memberSelect = new Member();
        memberSelect.setId(id);
        Member member = memberService.getOne(memberSelect);
        if(member == null){
            response.setError();
            return response;
        }
        response.setSuccess(member);
        return  response;
    }

    @GetMapping(path = "/members/",params = {"page","rows"})
    public Response list(Member member,@RequestParam  Integer page,@RequestParam  Integer rows){
        Response response = new Response();
        EUDataGridResult result = memberService.list(member,page,rows);
        response.setSuccess(result);
        return  response;
    }


    @DeleteMapping("/members/{id}")
    public Response delete(@PathVariable Integer id){
        Response response = new Response();
        int num = memberService.removePhysicalById(id);
        if(num <= 0){
            response.setError();
            return response;
        }
        response.setSuccess();
        return  response;
    }

}
